home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_include / ASM-PPC / UACCESS.H < prev    next >
C/C++ Source or Header  |  1999-09-17  |  8KB  |  267 lines

  1. #ifndef _PPC_UACCESS_H
  2. #define _PPC_UACCESS_H
  3.  
  4. #ifndef __ASSEMBLY__
  5. #include <linux/sched.h>
  6. #include <linux/errno.h>
  7. #include <asm/processor.h>
  8.  
  9. #define VERIFY_READ    0
  10. #define VERIFY_WRITE    1
  11.  
  12. /*
  13.  * The fs value determines whether argument validity checking should be
  14.  * performed or not.  If get_fs() == USER_DS, checking is performed, with
  15.  * get_fs() == KERNEL_DS, checking is bypassed.
  16.  *
  17.  * For historical reasons, these macros are grossly misnamed.
  18.  */
  19.  
  20. #define KERNEL_DS    ((mm_segment_t) { 0 })
  21. #define USER_DS        ((mm_segment_t) { 1 })
  22.  
  23. #define get_ds()    (KERNEL_DS)
  24. #define get_fs()    (current->tss.fs)
  25. #define set_fs(val)    (current->tss.fs = (val))
  26.  
  27. #define segment_eq(a,b)    ((a).seg == (b).seg)
  28.  
  29. #define __kernel_ok (segment_eq(get_fs(), KERNEL_DS))
  30. #define __user_ok(addr,size) (((size) <= TASK_SIZE)&&((addr) <= TASK_SIZE-(size)))
  31. #define __access_ok(addr,size) (__kernel_ok || __user_ok((addr),(size)))
  32. #define access_ok(type,addr,size) __access_ok((unsigned long)(addr),(size))
  33.  
  34. extern inline int verify_area(int type, const void * addr, unsigned long size)
  35. {
  36.     return access_ok(type,addr,size) ? 0 : -EFAULT;
  37. }
  38.  
  39.  
  40. /*
  41.  * The exception table consists of pairs of addresses: the first is the
  42.  * address of an instruction that is allowed to fault, and the second is
  43.  * the address at which the program should continue.  No registers are
  44.  * modified, so it is entirely up to the continuation code to figure out
  45.  * what to do.
  46.  *
  47.  * All the routines below use bits of fixup code that are out of line
  48.  * with the main instruction path.  This means when everything is well,
  49.  * we don't even have to jump over them.  Further, they do not intrude
  50.  * on our cache or tlb entries.
  51.  */
  52.  
  53. struct exception_table_entry
  54. {
  55.     unsigned long insn, fixup;
  56. };
  57.  
  58. /* Returns 0 if exception not found and fixup otherwise.  */
  59. extern unsigned long search_exception_table(unsigned long);
  60.  
  61.  
  62. /*
  63.  * These are the main single-value transfer routines.  They automatically
  64.  * use the right size if we just have the right pointer type.
  65.  *
  66.  * This gets kind of ugly. We want to return _two_ values in "get_user()"
  67.  * and yet we don't want to do any pointers, because that is too much
  68.  * of a performance impact. Thus we have a few rather ugly macros here,
  69.  * and hide all the uglyness from the user.
  70.  *
  71.  * The "__xxx" versions of the user access functions are versions that
  72.  * do not verify the address space, that must have been done previously
  73.  * with a separate "access_ok()" call (this is used when we do multiple
  74.  * accesses to the same area of user memory).
  75.  *
  76.  * As we use the same address space for kernel and user data on the
  77.  * PowerPC, we can just do these as direct assignments.  (Of course, the
  78.  * exception handling means that it's no longer "just"...)
  79.  */
  80. #define get_user(x,ptr) \
  81.   __get_user_check((x),(ptr),sizeof(*(ptr)))
  82. #define put_user(x,ptr) \
  83.   __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
  84.  
  85. #define __get_user(x,ptr) \
  86.   __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
  87. #define __put_user(x,ptr) \
  88.   __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))
  89.  
  90. /*
  91.  * The "xxx_ret" versions return constant specified in third argument, if
  92.  * something bad happens. These macros can be optimized for the
  93.  * case of just returning from the function xxx_ret is used.
  94.  */
  95.  
  96. #define put_user_ret(x,ptr,ret) ({ \
  97. if (put_user(x,ptr)) return ret; })
  98.  
  99. #define get_user_ret(x,ptr,ret) ({ \
  100. if (get_user(x,ptr)) return ret; })
  101.  
  102. #define __put_user_ret(x,ptr,ret) ({ \
  103. if (__put_user(x,ptr)) return ret; })
  104.  
  105. #define __get_user_ret(x,ptr,ret) ({ \
  106. if (__get_user(x,ptr)) return ret; })
  107.  
  108.  
  109. extern long __put_user_bad(void);
  110.  
  111. #define __put_user_nocheck(x,ptr,size)            \
  112. ({                            \
  113.     long __pu_err;                    \
  114.     __put_user_size((x),(ptr),(size),__pu_err);    \
  115.     __pu_err;                    \
  116. })
  117.  
  118. #define __put_user_check(x,ptr,size)                \
  119. ({                                \
  120.     long __pu_err = -EFAULT;                \
  121.     __typeof__(*(ptr)) *__pu_addr = (ptr);            \
  122.     if (access_ok(VERIFY_WRITE,__pu_addr,size))        \
  123.         __put_user_size((x),__pu_addr,(size),__pu_err);    \
  124.     __pu_err;                        \
  125. })
  126.  
  127. #define __put_user_size(x,ptr,size,retval)            \
  128. do {                                \
  129.     retval = 0;                        \
  130.     switch (size) {                        \
  131.       case 1: __put_user_asm(x,ptr,retval,"stb"); break;    \
  132.       case 2: __put_user_asm(x,ptr,retval,"sth"); break;    \
  133.       case 4: __put_user_asm(x,ptr,retval,"stw"); break;    \
  134.       default: __put_user_bad();                \
  135.     }                            \
  136. } while (0)
  137.  
  138. struct __large_struct { unsigned long buf[100]; };
  139. #define __m(x) (*(struct __large_struct *)(x))
  140.  
  141. /*
  142.  * We don't tell gcc that we are accessing memory, but this is OK
  143.  * because we do not write to any memory gcc knows about, so there
  144.  * are no aliasing issues.
  145.  */
  146. #define __put_user_asm(x, addr, err, op)            \
  147.     __asm__ __volatile__(                    \
  148.         "1:    "op" %1,0(%2)\n"            \
  149.         "2:\n"                        \
  150.         ".section .fixup,\"ax\"\n"            \
  151.         "3:    li %0,%3\n"                \
  152.         "    b 2b\n"                    \
  153.         ".section __ex_table,\"a\"\n"            \
  154.         "    .align 2\n"                \
  155.         "    .long 1b,3b\n"                \
  156.         ".text"                        \
  157.         : "=r"(err)                    \
  158.         : "r"(x), "b"(addr), "i"(-EFAULT), "0"(err))
  159.  
  160.  
  161. #define __get_user_nocheck(x,ptr,size)                \
  162. ({                                \
  163.     long __gu_err, __gu_val;                \
  164.     __get_user_size(__gu_val,(ptr),(size),__gu_err);    \
  165.     (x) = (__typeof__(*(ptr)))__gu_val;            \
  166.     __gu_err;                        \
  167. })
  168.  
  169. #define __get_user_check(x,ptr,size)                    \
  170. ({                                    \
  171.     long __gu_err = -EFAULT, __gu_val = 0;                \
  172.     const __typeof__(*(ptr)) *__gu_addr = (ptr);            \
  173.     if (access_ok(VERIFY_READ,__gu_addr,size))            \
  174.         __get_user_size(__gu_val,__gu_addr,(size),__gu_err);    \
  175.     (x) = (__typeof__(*(ptr)))__gu_val;                \
  176.     __gu_err;                            \
  177. })
  178.  
  179. extern long __get_user_bad(void);
  180.  
  181. #define __get_user_size(x,ptr,size,retval)            \
  182. do {                                \
  183.     retval = 0;                        \
  184.     switch (size) {                        \
  185.       case 1: __get_user_asm(x,ptr,retval,"lbz"); break;    \
  186.       case 2: __get_user_asm(x,ptr,retval,"lhz"); break;    \
  187.       case 4: __get_user_asm(x,ptr,retval,"lwz"); break;    \
  188.       default: (x) = __get_user_bad();            \
  189.     }                            \
  190. } while (0)
  191.  
  192. #define __get_user_asm(x, addr, err, op)        \
  193.     __asm__ __volatile__(                \
  194.         "1:    "op" %1,0(%2)\n"        \
  195.         "2:\n"                    \
  196.         ".section .fixup,\"ax\"\n"        \
  197.         "3:    li %0,%3\n"            \
  198.         "    li %1,0\n"            \
  199.         "    b 2b\n"                \
  200.         ".section __ex_table,\"a\"\n"        \
  201.         "    .align 2\n"            \
  202.         "    .long 1b,3b\n"            \
  203.         ".text"                    \
  204.         : "=r"(err), "=r"(x)            \
  205.         : "b"(addr), "i"(-EFAULT), "0"(err))
  206.  
  207. /* more complex routines */
  208.  
  209. extern int __copy_tofrom_user(void *to, const void *from, unsigned long size);
  210.  
  211. extern inline unsigned long
  212. copy_from_user(void *to, const void *from, unsigned long n)
  213. {
  214.     if (access_ok(VERIFY_READ, from, n))
  215.         return __copy_tofrom_user(to, from, n);
  216.     return n;
  217. }
  218.  
  219. extern inline unsigned long
  220. copy_to_user(void *to, const void *from, unsigned long n)
  221. {
  222.     if (access_ok(VERIFY_WRITE, to, n))
  223.         return __copy_tofrom_user(to, from, n);
  224.     return n;
  225. }
  226.  
  227. #define copy_to_user_ret(to,from,n,retval) ({ if (copy_to_user(to,from,n)) return retval; })
  228.  
  229. #define copy_from_user_ret(to,from,n,retval) ({ if (copy_from_user(to,from,n)) return retval; })
  230.  
  231. #define __copy_from_user(to, from, size) \
  232.     __copy_tofrom_user((to), (from), (size))
  233. #define __copy_to_user(to, from, size) \
  234.     __copy_tofrom_user((to), (from), (size))
  235.  
  236. extern unsigned long __clear_user(void *addr, unsigned long size);
  237.  
  238. extern inline unsigned long
  239. clear_user(void *addr, unsigned long size)
  240. {
  241.     if (access_ok(VERIFY_WRITE, addr, size))
  242.         return __clear_user(addr, size);
  243.     return size? -EFAULT: 0;
  244. }
  245.  
  246. extern int __strncpy_from_user(char *dst, const char *src, long count);
  247.  
  248. extern inline long
  249. strncpy_from_user(char *dst, const char *src, long count)
  250. {
  251.     if (access_ok(VERIFY_READ, src, 1))
  252.         return __strncpy_from_user(dst, src, count);
  253.     return -EFAULT;
  254. }
  255.  
  256. /*
  257.  * Return the size of a string (including the ending 0)
  258.  *
  259.  * Return 0 for error
  260.  */
  261.  
  262. extern long strlen_user(const char *);
  263.  
  264. #endif  /* __ASSEMBLY__ */
  265.  
  266. #endif    /* _PPC_UACCESS_H */
  267.